package gt.controller.wx;

import gt.common.UrlConstant;
import gt.entity.payRecord.PayRecord;
import gt.util.HttpHelper;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.Callable;

import net.sf.json.JSONObject;

/**
 * 发送微信支付成功，传递信息
 * @author wenyu
 * @date 2017年9月30日 下午12:34:28
 */
public class wxPayCallable  implements Callable<JSONObject>{
	

		private PayRecord payRecord;
		private  String payNumber;
		
		public wxPayCallable(PayRecord payRecord, String payNumber){
			this.payRecord = payRecord;
			this.payNumber = payNumber;
		}
		
		@Override
		 public JSONObject call() throws Exception {
			// TODO Auto-generated method stub
			System.out.println("-----进入微信支付成功后的数据处理线程-----");
				JSONObject obj = new JSONObject();
				try {
					obj.put("incomeMoney", payRecord.getWxPayMoney());// 支付金额
					obj.put("outStandingBalance", payRecord.getNotReceiveMoney());// 未收金额
					obj.put("sysIncomeMoney", payRecord.getReceiveMoney());// 应收金额
					obj.put("agentId", payRecord.getAgentId());// 站点Id
					obj.put("carrierId", payRecord.getCarrierId());// 快递公司Id
					obj.put("carrierSiteId", payRecord.getCarrierSiteId());// 子快递公司Id
					obj.put("agentName",
							URLEncoder.encode(URLEncoder.encode(
									payRecord.getSettlementSite(), "UTF-8")));// 站点名称
					obj.put("cityId", payRecord.getCityId());// 子快递公司Id
					obj.put("cityName", 
							URLEncoder.encode(URLEncoder.encode(
									payRecord.getCityName(), "UTF-8")));// 子快递公司Id
					obj.put("month", payRecord.getSettlementCycle());// 结算周期
					obj.put("payStatus", 1);// 结算状态
					obj.put("receiveTime", System.currentTimeMillis());// 收款时间(微信回调时给的)（若没有时间则返回当前时间）===========
					obj.put("payTime", payRecord.getCreateTime());// 付款时间
					obj.put("createUser", payRecord.getOpenId());// 微信用户唯一标识
					obj.put("instanceId", payRecord.getPayId());// 支付记录唯一标识
					obj.put("orderNumber", payRecord.getPayNo());// 订单号 orderNumber(payNo)
					obj.put("payNumber", payNumber);// 微信回调后的单号(微信回调时给的)=============
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-----支付成功回调接口参数："+obj);
				String urlString = UrlConstant.NOTICE_SERVER_URL + "?params=" + obj;
				// 调sercer接口 通知server
	    		// 返回result：0成功,1:接口异常, 2：未获取到参数信息，3：未获取到收款金额，4：未获取到服务站Id
				obj = HttpHelper.sendHttp(urlString);
				System.out.println("-----支付成功回调接口返回值："+obj);
				return obj;
				
		 }
		
		
		/**
		 * 测试主函数
		 * @param args
		 */
		/*		 public static void main(String[] args) {
				 
			 
				 PayRecord record = new PayRecord();
				 record.setPayNo(System.currentTimeMillis() + "");
					record.setPayMoney(new BigDecimal(1));
					record.setReceiveMoney(new BigDecimal(1));
					record.setCarrierId(1L);
					record.setCarrierSiteId(1L);//子快递公司Id
					record.setAgentId(1L);
					record.setOpenId("11111");
					record.setCityId(1L);
					record.setCityName("11111");
					record.setNotReceiveMoney(new BigDecimal(1));
					record.setSettlementCycle("20170001");// 结算周期
					record.setSettlementSite("10111");// 结算站点
					record.setPayStatus(1);// 支付状态0待支付，1已确认支付，2支付失败 3取消收款
					record.setIsToldServer(1);// 支付成功后通知server系统(0:未通知1:已通知）
					record.setCreateTime(System.currentTimeMillis());
					record.setUpdateTime(System.currentTimeMillis());
		    		record.setPayStatus(1);// 支付状态0待支付，1已确认支付，2支付失败 3取消收款
		    		record.setWxPayMoney(new BigDecimal(1).divide(new BigDecimal(100)));//"分" 转 "元"
		    		
		    		
				 String payNumber = "111111";
					// 微信支付线程池
				Executor wxPay_ex = Executors.newFixedThreadPool(10);
				
				 JSONObject jsonObject = new JSONObject();
				 CompletionService<JSONObject> completionService=new ExecutorCompletionService<JSONObject>(wxPay_ex);
	    			
	    			try {
	    				
		    			System.out.println("-----微信支付成功后的数据处理线程执行成功-----"+jsonObject);
		    			for(int i=0;i<3;i++){
		    				
		    				Future<JSONObject> future=completionService.submit(new wxPayCallable(record,payNumber));
							jsonObject = future.get();
							
							
			    			if (("0").equals(jsonObject.getString("result"))) {
			        			record.setIsToldServer(1);// 支付成功后通知server系统(0:未通知1:已通知）
			        			record.setUpdateTime(System.currentTimeMillis());
			        			break;
			    			}else{
			    	        	String errMsg = jsonObject.getString("message");
			    	        	  System.out.println("-------return_msg------"+errMsg);
			    	        	//失败，休息一分钟
			  					Thread.sleep(60000L);
			    	        }
		    			}
	    			} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			 }*/
}

